package com.priox.sigar.application.job.mappers;

import com.priox.sigar.client.dto.AcreedorDTO;
import com.priox.sigar.client.dto.ClienteDTO;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;

/**
 * Created with IntelliJ IDEA.
 * User: Johann
 * Date: 28/08/13
 * Time: 11:13 PM
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ClienteFieldSetMapper implements FieldSetMapper<ClienteDTO> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ClienteFieldSetMapper.class);
    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */
    @Override
    public ClienteDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        ClienteDTO result = new ClienteDTO();
        try {
            if(NumberUtils.isNumber(fieldSet.readString(1))
                    && StringUtils.isNotBlank(fieldSet.readString(2))
                    && fieldSet.readString(1).length()==3){
                result.setSociedadId((fieldSet.readString(1)));
                result.setClienteId((fieldSet.readString(2)));
                result.setRazonSocial(fieldSet.readString(3));
            }
        } catch (Exception ex) {
            LOGGER.error("Error al leer cliente "+fieldSet);
        }
        return result;
    }
}

